import Vue from 'vue'
import App from './App.vue'
import router from './router'
import store from './store'
import moment from 'moment'
// 引入ElementUI
import ElementUI from 'element-ui'
import 'element-ui/lib/theme-chalk/index.css'
// 引入axios
import axios from "@/http/http"
import { Message } from "element-ui"

// 将api.js挂载到全局
Vue.prototype.$api = require("@/http/api")

Vue.prototype.$http = axios
Vue.prototype.$moment = moment // 日期格式化
Vue.use(ElementUI)

// 配置axios响应拦截器
axios.interceptors.response.use(
  response => {
    console.log("进入到了响应拦截器：", response, response.data.code)
    // 当token过期或者不存在时，后台返回状态码为202，此时页面跳转到登录页面
    if (response.status === 401 || response?.data?.code == 202) {
      console.log(response.data.code, router, this)
      Message.error(response?.data?.msg)
      router.replace({ path: "/login" })
    }
    return response
  },
  error => {
    return Promise.reject(error)
  }

)

Vue.config.productionTip = false

new Vue({
  router,
  store,
  render: h => h(App)
}).$mount('#app')
